<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

require_once dirname(__FILE__).'/menu.php';

class Group extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Groupmodel','m_group');
	}

	public function index(){
		$data = parent::index();
		$user = $this->session->userdata('user');
		$data['group_level'] = $user['group_level'];
		$this->load->view('admin/group',$data);
	}

	public function getGroupList(){
		$assist['pagination'] = getPagination($this->input->post());
		list($list,$count) = $this->m_group->getGroupListSection($assist);
		foreach($list as $key => $record){
			$list[$key]['action_count'] = substr_count($record['group_action'], '/');
		}

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function createGroup(){
		$this->validate->setRule('group_name','string');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		
		$this->m_group->createGroup(array(
			'group_name' => $info['group_name'],
		));
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editGroup(){
		$this->validate->setRule('group_id','integer');
		$this->validate->setRule('group_name','string');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		$this->m_group->editGroup($info);
		
		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function getGroupActionTree(){
		$this->validate->setRule('group_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		
		$group = $this->m_group->getGroup($info['group_id']);
		

		$group_action = array();
		if(isset($group['group_action'])) {
			$group_action = $group['group_action'];
			if(!empty($group_action)) {
				$group_action = explode(',', $group_action);
			} else {
				$group_action = array();
			}
		}

		$tree = $this->m_group->getGroupActionTree($group_action);

		$this->response->setSuccess();
		$this->response->setItems($tree);
		$this->response->responseExit();
	}

	public function editGroupAction(){
		$this->validate->setRule('group_id','integer');
		$this->validate->setRule('group_action','array');
		$this->validate->setRule('group_action','optional');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		if(!isset($info['group_action'])) {
			$info['group_action'] = array();
		}
		$info['group_action'] = implode(',', $info['group_action']);
		
		$this->m_group->editGroup($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editGroupLevel() {
		$this->validate->setRule('group_id','integer');
		$this->validate->setRule('group_level','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		
		$this->m_group->editGroup($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

}
/* End of file group.php */
/* Location: ./application/controllers/group.php */